﻿using System;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Data;

namespace Kotonoha.Server.UI.Converters {
    public class DoubleNormalizerConverter : IValueConverter {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
            // Check for design mode. 
            if ((bool)(DesignerProperties.IsInDesignModeProperty.GetMetadata(typeof(DependencyObject)).DefaultValue)) {
                return process(parameter);
            }
            return process(value ?? parameter);
        }

        private static object process(object value) {
            if (value == null) {
                return null;
            }

            if (value is string) {
                string s = (string) value;
                return double.Parse(s);
            }
            return (double) value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
            return value;
        }
    }
}
